package com.swipemaster.network.task;

import android.app.Activity;

import com.swipemaster.dialogs.Dialogs;
import com.swipemaster.dialogs.RecordsDialog;
import com.swipemaster.network.Network;
import com.swipemaster.record.Record;

/** Submits your record and shows your position in global ranking. */
public class SubmitRecordTask extends ProgressTask<Void, Void, Integer>
{
	private final Record record;
	private final String username;

	public SubmitRecordTask(Activity activity, Record record, String username)
	{
		super(activity);
		this.record = record;
		this.username = username;
	}

	@Override
	protected Integer doInBackground(Void... params)
	{
		try
		{
			final int rank = Network.sendRecord(record, username);
			RecordsDialog.refreshList(rank);
			return rank;
		}
		catch (final Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	@Override
	protected void onPostExecute(Integer result)
	{
		if (result == null)
		{
			Dialogs.showInternetError(activity);
			return;
		}
		super.onPostExecute(result);
		Dialogs.showAfterSubmit(activity, result);
	}
}